﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.BGM;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Web.Mvc;
using KPIS.GERP.Models.Class;

namespace KPIS.GERP.WCF.GBMS
{
    [ServiceContract]
    public interface IProjectOperationService
    {
        [OperationContract]
        List<SelectListItem> SelectList(ProjectOperation criteriaModel = null, int? EmpId = null);

        [OperationContract]
        List<SelectListItem> SelectListExpense(ProjectOperationExpense criteriaModel = null);

        [OperationContract]
        IEnumerable<ProjectOperationExpense> SelectExpense(ProjectOperationExpense criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IEnumerable<ProjectExpense> SelectProjectExpense(ProjectExpense criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        void UpdateProjectOperationReportToZero(int BgmProjOperRptSeq, int EmpSeq);
    }

    [ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class ProjectOperationService : IProjectOperationService
    {
        public List<SelectListItem> SelectList(ProjectOperation criteriaModel = null, int? EmpId = null)
        {
            var data = new List<SelectListItem>();
            data.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var db = new GERPContext(false))
                {
                    List<ProjectOperationPerson> queryPersons = new List<ProjectOperationPerson>();
                    if (EmpId != null)
                    {
                        queryPersons = db.ProjectOperationPersons.Where(m => m.EmpId == EmpId).ToList();
                    }

                    IQueryable<ProjectOperation> query = db.ProjectOperations.Include(o=>o.JobSite);

                    List<ProjectOperation> models = new List<ProjectOperation>();
                    if (criteriaModel != null)
                    {
                        var recordStatus = criteriaModel.RecordStatus;
                        criteriaModel.RecordStatus = null;
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                        if (recordStatus != null && recordStatus == "A")
                        {
                            query = query.Where(o => o.RecordStatus == "A" || o.RecordStatus == "U");
                        }
                        models = query.ToList();
                    }

                    foreach (var model in models)
                    {
                        if (queryPersons.Count > 0)
                        {
                            foreach (var queryPerson in queryPersons)
                            {
                                if (model.ProjOperId == queryPerson.ProjOperId)
                                {
                                    var item = new SelectListItem { Text = model.Name, Value = model.ProjOperId.ToString() };
                                    data.Add(item);
                                }
                            }
                        }
                        else
                        {
                            var item = new SelectListItem { Text = model.Name, Value = model.ProjOperId.ToString() };
                            data.Add(item);
                        }
                    }

                    return data;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<SelectListItem> SelectListExpense(ProjectOperationExpense criteriaModel = null)
        {
            var data = new List<SelectListItem>();
            data.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var db = new GERPContext(false))
                {
                    IQueryable<ProjectOperationExpense> query = db.ProjectOperationExpenses.Include("ProjectExpense.MasBudgetExpense").Include("ProjectExpense.MasBudgetSource").Include("ProjectOperation");

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    query = query.Where(m => m.ProjectExpense.MasBudgetExpense.BgExpGrpIdReal == 2 || m.ProjectExpense.MasBudgetExpense.BgExpGrpIdReal == 3);

                    var models = query.ToList();

                    foreach (var model in models)
                    {
                        var item = new SelectListItem { Text = model.ProjectExpense.MasBudgetSource.Name + " : " + model.ProjectExpense.MasBudgetExpense.Name + "[" + model.RemainAmount.ToString("#,##0.00") + "]", Value = model.ProjOperExpId.ToString() };
                        data.Add(item);
                    }

                    return data;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IEnumerable<ProjectOperationExpense> SelectExpense(ProjectOperationExpense criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<ProjectOperationExpense> query = context.ProjectOperationExpenses;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        criteriaModel.RemainAmount = 0;
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }
                    
                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IEnumerable<ProjectExpense> SelectProjectExpense(ProjectExpense criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<ProjectExpense> query = context.ProjectExpenses;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateProjectOperationReportToZero(int PrItemId, int EmpSeq)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    var model = context.ProjectOperationReports.Where(m => m.PrItemId == PrItemId).FirstOrDefault();

                    context.Entry(model).State = EntityState.Modified;
                    model.CalAmount = 0;
                    model.UpdatedBy = EmpSeq;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
